<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2024/3/11
 * Time: 11:21
 */

namespace app\common\helper;


use app\common\enum\BaseEnum;
use app\common\enum\SetJlpjkFlsx_lxEnum;
use app\common\service\FlsxServer;
use app\modules\setting\models\jlpjk\SetJlpjkFlgl;
use app\modules\setting\models\jlpjk\SetJlpjkFlsx;
use app\modules\setting\models\jlpjk\SetJlpjkWzgl;

class TreeMapHelper extends BaseEnum
{
    const TREE_SHE_BEI = 'tree_sb';
    const TREE_SHU_XING = 'tree_sx';
    const TREE_WEI_ZHI = 'tree_wz';
    const TREE_WEI_ZHI_EXT_FL_CONFIG = 'tree_wz_ext_fl_config';
    public function treeMap(){
        return [
            self::TREE_SHE_BEI => TreeMapItem::createInstance()
                ->setTreeClass(SetJlpjkFlgl::class)
                ->setTreeFieldId("id")
                ->setTreeFieldName("mc")
                ->setTreeFieldPid("pid")
                ->setTreeSoftField("soft")
                ->setLeafClass(SetJlpjkWzgl::class)
                ->setLeafFieldMap("fl_id")
                ->setLeafFieldId("id")
                ->setLeafFieldName("mc")
                ->setLeafSoftField("soft"),

            self::TREE_SHU_XING => TreeMapItem::createInstance()
                ->setTreeClass(SetJlpjkFlgl::class)
                ->setTreeFieldId("id")
                ->setTreeFieldName("mc")
                ->setTreeFieldPid("pid")
                ->setTreeSoftField("soft")
                ->setLeafClass(SetJlpjkFlsx::class)
                ->setLeafFieldMap("fl_id")
                ->setLeafFieldId("id")
                ->setLeafFieldName("mc")
                ->setLeafSoftField("soft")
                ->setLeafWhere(['lx' => [
                    SetJlpjkFlsx_lxEnum::TYPE_LIST_CHECKBOX,
                    SetJlpjkFlsx_lxEnum::TYPE_LIST_RADIO,
                    SetJlpjkFlsx_lxEnum::TYPE_LIST_SELECT,
                ]]),

            self::TREE_WEI_ZHI => TreeMapItem::createInstance()
                ->setTreeClass(SetJlpjkFlgl::class)
                ->setTreeFieldId("id")
                ->setTreeFieldName("mc")
                ->setTreeFieldPid("pid")
                ->setTreeSoftField("soft")
                ->setLeafClass(SetJlpjkWzgl::class)
                ->setLeafFieldMap("fl_id")
                ->setLeafFieldId("id")
                ->setLeafFieldName("mc")
                ->setLeafSoftField("soft"),

            self::TREE_WEI_ZHI_EXT_FL_CONFIG => TreeMapItem::createInstance()
                ->setTreeClass(SetJlpjkFlgl::class)
                ->setTreeFieldId("id")
                ->setTreeFieldName("mc")
                ->setTreeFieldPid("pid")
                ->setTreeSoftField("soft")
                ->setLeafClass(SetJlpjkWzgl::class)
                ->setLeafFieldMap("fl_id")
                ->setLeafFieldId("id")
                ->setLeafFieldName("mc")
                ->setLeafSoftField("soft")
                ->setExtServer(FlsxServer::class),
        ];
    }





    public function getItem($type){
        if(static::isValid($type)){
            $map = $this->treeMap();
            if(isset($map[$type])){
                $item = $map[$type];
                return $item;
            }
        }
        return null;
    }


    public function getOptions($type = ''){
        if(empty($type)){
            $type = $this->getValue();
        }
        if(!static::isValid($type)){
            return [];
        }
        $key = md5("TreeMapHelper{$type}");
        $data = \Yii::$app->cache->get($key);
        if($data!==false){
            return $data;
        }
        $item = $this->getItem($type);
        if($item instanceof TreeMapItem){
            $data = $item->getOptions();
            if($data){
                \Yii::$app->cache->set($key,$data,60);
                return $data;
            }
        }
        return [];
    }


    public function __construct($value)
    {
        parent::__construct($value);
    }
    public static $obj ;
    public static function getInstance($value){
        return self::$obj ? self::$obj : self::$obj = new static($value);
    }
}